package polsense.filemerger;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.zip.CRC32;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.IOFileFilter;

/* FileScanner.java
 * Created on Oct 12, 2005
 * Copyright 2005 Pablo J. Lopez
 */

/**
 * @author Pablo J. Lopez
 */
public class FileScanner implements Runnable {

	private Collection sink;
	private String[] extensions;
	private File directory;
	private boolean finished = false;
	
	
	
	/**
	 * @param directory
	 * @param sink
	 * @param extensions
	 */
	public FileScanner(File directory, Collection sink, String[] extensions) {
		super();
		// TODO Auto-generated constructor stub
		this.directory = directory;
		this.sink = sink;
		this.extensions = extensions;
	}

	

	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	public void run() {
		
            try{
            System.out.println("Scanner Running for "+directory.getAbsolutePath());
		
            
        FileUtils.listFiles(directory, new IOFileFilter() {
        
        public boolean accept(File arg0) {
        	// TODO Auto-generated method stub
        	return false;
        }public boolean accept(File arg0, String arg1) {
        	
        	return false;
        };
        	}, new IOFileFilter() {
        
        public boolean accept(File arg0) {
        	// TODO Auto-generated method stub
        	return false;
        }public boolean accept(File arg0, String arg1) {
        	
        	return false;
        };
        	}
        );    
            
        Collection a = FileUtils.listFiles(directory,extensions,true);
		Collection b = new ArrayList(a.size());
		int count = 0;
		for (Iterator iter = a.iterator(); iter.hasNext();) {
			try {
				File element = (File) iter.next();
				long crc = MP3CRC.getChecksumValue(new CRC32(),element,4096);
				b.add(new CRCFile(element.getAbsolutePath(),crc));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
				
		}
		
		
		
		sink.addAll(b);
		a = null;
		b = null;
            } finally {finished = true;} 
	}



	/**
	 * @return Returns the finished.
	 */
	public boolean isFinished() {
		return finished;
	}



	
	

}
